<?php
/* tblSubCategoryAndProduct */
class SubCategoryAndProduct extends Form
	{
		protected $_ID                 = array();
		protected $_SubCategory        = array();
		protected $_Product            = array();

		function __construct()
		{
			parent::__construct();
			//$this->getRow();
		}
		public function get_ID()
		{
				return  $this->_get_IDValue();
		}

		private function _get_IDValue()
		{
			if(isset($this->_ID['value']))
				return $this->_ID['value'];
			else
				return false;
		}

		public function get_SubCategory()
		{
				return  $this->_get_SubCategoryValue();
		}

		private function _get_SubCategoryValue()
		{
			if(isset($this->_SubCategory['value']))
				return $this->_SubCategory['value'];
			else
				return false;
		}

		public function get_Product()
		{
				return  $this->_get_ProductValue();
		}

		private function _get_ProductValue()
		{
			if(isset($this->_Product['value']))
				return $this->_Product['value'];
			else
				return false;
		}


		public function set_IDValue($strID)
		{
			if($this->validateInput("validatexxxxx", $strID, "ID "))
					return($this->_setNewValue($this->_ID, $strID));
			else
					return false;
		}

		public function set_SubCategoryValue($strSubCategory)
		{
			if($this->validateInput("validateNumber", $strSubCategory, "SubCategory "))
					return($this->_setNewValue($this->_SubCategory, $strSubCategory));
			else
					return false;
		}

		public function set_ProductValue($strProduct)
		{
			if($this->validateInput("validateNumber", $strProduct, "Product "))
					return($this->_setNewValue($this->_Product, $strProduct));
			else
					return false;
		}
		public function updateTblSubCategoryAndProduct()
		{
			if($this->_boFailedValidation)
					return false;
					$strErrorMsg = "";
					$strSuccessMsg = "";
			if($this->get_ID() == "")
					return $this->_setFailScreens("Update aborted no record selected.", true);

			$strJoin = "";
			$strSql = "UPDATE tblSubCategoryAndProduct
					SET ";
					if($this->_ID['updated'])
					{
						$strSql .= $strJoin." ID = '".$this->_ID['value']."' ";
						$strJoin = ",";
						$strSuccessMsg .= "ID changed from '".$this->_ID['oldvalue']."' to '".$this->_ID['value']."'<br/>";
						$strErrorMsg .= "ID not updated<br/>";
					}

					if($this->_SubCategory['updated'])
					{
						$strSql .= $strJoin." SubCategory = '".$this->_SubCategory['value']."' ";
						$strJoin = ",";
						$strSuccessMsg .= "SubCategory changed from '".$this->_SubCategory['oldvalue']."' to '".$this->_SubCategory['value']."'<br/>";
						$strErrorMsg .= "SubCategory not updated<br/>";
					}

					if($this->_Product['updated'])
					{
						$strSql .= $strJoin." Product = '".$this->_Product['value']."' ";
						$strJoin = ",";
						$strSuccessMsg .= "Product changed from '".$this->_Product['oldvalue']."' to '".$this->_Product['value']."'<br/>";
						$strErrorMsg .= "Product not updated<br/>";
					}

			$strSql .= "WHERE ID = ". $this->get_ID();
			//Only execute sql if there is something to update.
			if($strJoin == ",")
			{
				//echo nl2br($strSql);
				if(!($DatabaseResult = mysql_query($strSql)))
					return $this->_setFailScreens($strErrorMsg);
				$GLOBALS['arrSuccessMessages'][] = $strSuccessMsg;
			}else
				$GLOBALS['arrSuccessMessages'][] = "No data has changed, so no updates made.";
			return(TRUE);
		}
			public function delete_tblSubCategoryAndProduct()
			{
				if($this->get_IDValue() == "")
					return $this->_setFailScreens("Delete aborted no record selected.", true);
				
		
				/* Deleted port forward from agg */
				$strSql =   "DELETE FROM tblSubCategoryAndProduct
		   					  WHERE Product		= ".$this->get_IDValue();
		
				$qryDeletetblSubCategoryAndProduct = new MySqlResults($strSql);
		
				if(!$qryDeletetblSubCategoryAndProduct->result)
					return ($this->_setFailScreens("An error occurred while trying to delete database entry."));
		
		
				$GLOBALS['arrSuccessMessages'][] = " has been removed";
				return true;
			}
		public function insertTblSubCategoryAndProduct()
		{
			/* Check that all required fields have been set */
			if (!($this->_validateAllFieldsSet()))
			{
				return ($this->_setFailScreens("Fields required to add a new record haven't all been set", true));
			}
			
			$strSql =   "INSERT INTO tblSubCategoryAndProduct
						  (		 	SubCategory
		, 	Product
)VALUE(
		 	'".$this->get_SubCategory()."' 
		, 	'".$this->get_Product()."' 
			)";
			
			$qryInserttblSubCategoryAndProduct = new MySqlResults($strSql);
		if ($qryInserttblSubCategoryAndProduct->result)
		{
			/*Extra post sql processing  */
		}else
			return $this->_setFailScreens("Unable to add to  tblSubCategoryAndProduct");
			
		return true;
	}
	/**
	 * public function to search tblSubCategoryAndProduct
	 * @param any that are set in $this
	 * @return MySqlResults
	 */
	public function searchSubCategoryAndProduct()
	{
		$strJoin = " WHERE ";
		$strSql = "SELECT * 
                   FROM tblSubCategoryAndProduct ";

		if($this->_get_IDValue())
		{
			$strSql .= $strJoin." ID = '".$this->_get_IDValue()."' ";
			$strJoin = " AND ";
		}

		if($this->_get_SubCategoryValue())
		{
			$strSql .= $strJoin." SubCategory = '".$this->_get_SubCategoryValue()."' ";
			$strJoin = " AND ";
		}

		if($this->_get_ProductValue())
		{
			$strSql .= $strJoin." Product = '".$this->_get_ProductValue()."' ";
			$strJoin = " AND ";
		}


		$qrySearchtblSubCategoryAndProduct = new MySqlResults($strSql);
		return $qrySearchtblSubCategoryAndProduct;	
    }
    
    /**
     * Get SubCategory that a Product belongs to. This currently
     * doesn't take into account that a Product could belong to more than 1
     * SubCategory
     * @param integer $iProd
     */
    public static function getProductSubCat($iProd)
    {
    	$objSubCatProd = new SubCategoryAndProduct();
    	if($objSubCatProd->set_ProductValue($iProd))
    	{
    		$qrySearch = $objSubCatProd->searchSubCategoryAndProduct();
    		if($qrySearch->result)
    			return $qrySearch->myrow['SubCategory'];
    	}
    	return false;
    }
}

?>